package com.xzh.wms.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.xzh.smart.core.exception.BizException;
import cn.xzh.smart.core.util.UserSessionContext;
import com.xzh.wms.dao.IoItemDao;
import com.xzh.wms.dao.IoMasterDao;
import com.xzh.wms.dao.PoMasterDao;
import com.xzh.wms.dto.InventoryOptionDTO;
import com.xzh.wms.entity.IoMaster;
import com.xzh.wms.entity.IoMasterQuery;
import com.xzh.wms.entity.PoMaster;
import com.xzh.wms.entity.PoMasterQuery;
import com.xzh.wms.enums.PoStatus;
import com.xzh.wms.event.InventoryOptionEvent;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 入库单头;(206_io_master)表服务实现类
 * @author : heimi
 * @date : 2024-11-28
 */
@Service
public class IoMasterService {
     
    @Autowired
    private IoMasterDao ioMasterDao;

    @Autowired
    private IoItemDao ioItemDao;

     @Autowired
     private PoMasterDao poMasterDao;

     @Autowired
     private ApplicationEventPublisher publisher;
    
    public List<IoMaster> select(IoMasterQuery query) {
        return ioMasterDao.select(query);
    }
    
    public Integer insert(IoMaster ioMaster) {
        String ioId = "IO" + DateUtil.format(DateUtil.date(), "yyyyMMddHHmmssSSS");
        ioMaster.setId(ioId);
        String poId = ioMaster.getPoId();
        PoMasterQuery poQuery = new PoMasterQuery().setId(poId);
        List<PoMaster> poMasters = poMasterDao.select(poQuery);
        if (ObjectUtil.isEmpty(poMasters)){
           throw  new BizException(454, "采购单不存在");
        }
        PoMaster poMaster = poMasters.get(0);
        ioMaster.setVendorName(poMaster.getVendorName());
        ioMaster.setVendorId(poMaster.getVendorId());
        ioMaster.setPoId(poId);
        ioMaster.setIoDate(LocalDateTimeUtil.now().toLocalDate());
        ioMaster.getIoItems().forEach(item->{
            item.setIoId(ioId);
            item.setLastUpdateBy(UserSessionContext.get().getNickName());
        });
        IoMasterService proxy = (IoMasterService)(AopContext.currentProxy());
        proxy.save(ioMaster);
        return 0;
    }

    @Transactional()
    public void save(IoMaster ioMaster) {
        ioItemDao.insertBatch(ioMaster.getIoItems());
        poMasterDao.update(new PoMaster().setId(ioMaster.getPoId()).setStatus(PoStatus.FINISH.getCode()));
        ioMasterDao.insert(ioMaster);
        InventoryOptionDTO optionDTO = new InventoryOptionDTO().builder().OptionType("in").refId(ioMaster.getId()).build();
        InventoryOptionEvent event = new InventoryOptionEvent(optionDTO);
        publisher.publishEvent(event);
    }
    
    public Integer update(IoMaster ioMaster) {
        return ioMasterDao.update(ioMaster);
    }
    
    public Integer delete(Integer id) {
        return ioMasterDao.delete(id);
    }
    
 
}